package com.gamex.findit.controller.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.gamex.findit.Constants;
import com.gamex.findit.internal.DefaultConfig;
import com.gamex.findit.model.MemberItem;
import com.gamex.findit.model.MemberSkill;
import com.gamex.findit.model.Skill;
import com.gamex.findit.service.CommonCodeService;
import com.gamex.findit.service.MemberSkillService;
import com.gamex.findit.service.SkillService;
import com.gamex.findit.util.CommonUtil;
import com.gamex.findit.util.Generator;

@Controller("adminSkill")
public class SkillController {
	
	private static final Logger logger = LoggerFactory.getLogger(SkillController.class);
	
	@Autowired SkillService skillService;
	@Autowired MemberSkillService memberSkillService;
	@Autowired CommonCodeService commonCodeService;
	@Autowired DefaultConfig config;
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param memberSkill
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/member/memberSkillList")
	public String memberItemList( HttpSession session
								, HttpServletRequest request
								, HttpServletResponse response
								, Model model
								, @ModelAttribute MemberSkill memberSkill) throws Exception{

		int count = memberSkillService.getMemberSkillCount(memberSkill);
		List<MemberSkill> result = memberSkillService.getMemberSkill(memberSkill);
		model.addAttribute("list", result);
		model.addAttribute("count", count);
		model.addAttribute("pageNo", memberSkill.getPageNo());
		model.addAttribute("pageGroup", memberSkill.getPageGroup());
		model.addAttribute("searchParam", memberSkill);
	return "/skill/memberSkillList";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param memberSkill
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/member/addMemberSkill")
	public String addMemberItem1( HttpSession session
							   , HttpServletRequest request
							   , HttpServletResponse response
							   , Model model
							   , @ModelAttribute MemberSkill memberSkill) throws Exception{
		
		memberSkillService.insertMemberSkill(memberSkill);
		model.addAttribute("searchParam", memberSkill);
	return "redirect:/admin/member/memberSkillList";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param skill
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/skillList")
	public String skillList( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, Model model
							, @ModelAttribute Skill skill) throws Exception{

		int count = skillService.getSkillCount(skill);
		List<Skill> result = skillService.getSkill(skill);
		model.addAttribute("list", result);
		model.addAttribute("count", count);
		model.addAttribute("pageNo", skill.getPageNo());
		model.addAttribute("pageGroup", skill.getPageGroup());
		model.addAttribute("searchParam", skill);
	return "/skill/skillList";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param skill
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/skillView")
	public String viewGameItem( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, ModelMap model
							, @ModelAttribute Skill skill) throws Exception{
		
		model.addAttribute("view", skillService.getSkillView(skill.getSkillId()));
		model.addAttribute("searchParam", skill);
	return "/skill/skillView";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param file
	 * @param skill
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/addSkill")
	public String addSkill( HttpSession session
						   , HttpServletRequest request
						   , HttpServletResponse response
						   , Model model
						   , @RequestParam("file") MultipartFile file
						   , @ModelAttribute Skill skill) throws Exception{
		
		String name = Generator.generateFileName();
		if(file.getSize() > 0){
			CommonUtil.writeImage(request, file, Constants.DEFAULT_SKILL_IMG_PATH, name);
			skill.setImg(String.format(Constants.DEFAULT_SKILL_URL, config.getHostAddress()) + name);
		}
		
		skillService.insertSkill(skill);
		
	return "redirect:/admin/skillList";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param file
	 * @param skill
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/updateSkill.*")
	public String updateSkill( HttpSession session
							  , HttpServletRequest request
							  , HttpServletResponse response
							  , Model model
							  , @RequestParam("file") MultipartFile file
							  , @ModelAttribute Skill skill) throws Exception{
		
		String name = Generator.generateFileName();
		if(file.getSize() > 0){
			CommonUtil.writeImage(request, file, Constants.DEFAULT_SKILL_IMG_PATH, name);
			skill.setImg(String.format(Constants.DEFAULT_SKILL_URL, config.getHostAddress()) + name);
		}
		
		skillService.updateSkill(skill);
		
	return "redirect:/admin/skillList";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param skill
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/deleteSkill")
	public String deleteSkill( HttpSession session
							  , HttpServletRequest request
							  , HttpServletResponse response
							  , Model model
							  , @ModelAttribute Skill skill) throws Exception{
		
		skillService.deleteSkill(skill.getSkillId());
		
	return "redirect:/admin/skillList";
	}
	
}
